<?php
/**
 * 装修公司
 */
namespace App\Http\ControllersAdmin;

use App\Service\ConfigsService;
use App\Validation\ConfigsSearchValidation;
use App\Validation\ConfigsValidation;
use App\Validation\DeleteValidation;
use App\Validation\HotSearchValidation;

class ConfigsController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new ConfigsSearchValidation, $this->request->getJsonRawBody());

		$data = ConfigsService::getList($raw);
		$d = [];
		foreach ($data as $key => $value) {
            $value->val = json_decode($value->val);
            $d[] = $value;
		}
		$count = ConfigsService::getList($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new ConfigsValidation, $this->request->getJsonRawBody());
		$data = ConfigsService::save($raw);
		return $this->echoJson('ok');
	}
    
    //获取
    public function keyAction(){
        
        $this->getUser();
		$raw = $this->request->getJsonRawBody();//$this->validation(new ConfigsValidation, $this->request->getJsonRawBody());
        $data = ConfigsService::getKeyVal($raw->key_name);
        return $this->echoJson($data);
    }

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$id = $this->dispatcher->getParam('id');
		$data = ConfigsService::getInfo($id);
		$data->val = json_decode($data->val);
		return $this->echoJson($data);
	}

	/**
	 * [valAction description]
	 * @return [type] [description]
	 */
	public function valAction()
	{
		$this->getUser();
		$raw = $this->validation(new HotSearchValidation, $this->request->getJsonRawBody());

		$data = ConfigsService::getVal($raw);
		$i = 0;
		foreach ($data as $key => $value) {
			
			$arr[$i]['id'] = $key;
			$arr[$i]['name'] = $value;
			$i ++;
		}
		// $data = array_chunk((array)$data, 1, TRUE);
		
		$assign = [
			"total" => count($arr),
			"list"  => $arr
		];
		return $this->echoJson($assign);
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new ConfigsValidation, $this->request->getJsonRawBody());

		$data = ConfigsService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = ConfigsService::forever($raw->id);
		return $this->echoJson('ok');
	}
}